import { FieldTypeEnum } from '@/api/enum';
import { VxeColumnProps } from '@/components/VxeTable';
import { Switch, type SelectProps, message } from 'ant-design-vue';
import { h } from 'vue';
import {
  changeStatus,
  changeIsMaster,
  changeIsAudit,
  changeIsSubmit
} from '@/api/application/apiConfig';

export interface ColumnsFrom extends VxeColumnProps {
  formHidden?: boolean;
  fieldType?: FieldTypeEnum;
  defaultValue?: string;
  isRow?: boolean;
  options: any;
  disabled: boolean;
  FkData?: SelectProps['options'];
  BoolRadioData?: [
    { value: string | number | boolean; label: string | number | boolean }
  ];
}

export default [
  { type: 'seq', width: 50, title: "序号", fixed: "left" },
  {
    title: '编码',
    field: 'code',
    visible: false,
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.Input,
    options: {
      allowClear: true
    }
  },
  {
    title: '名称',
    field: 'name',
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.Input
  },
  {
    title: '状态',
    field: 'status',
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.BooleanRadio,
    width: 80,
    BoolRadioData: [
      { value: 1, label: '启用' },
      { value: 2, label: '禁用' }
    ],
    //slots: { default: 'statusAction' },
    slots: {
      default: ({ row }) => {
        const record = row;
        if (!Reflect.has(record, 'pendingStatus')) {
          record.statusLoading = false;
        }
        return <Switch checked={record.status === 1} size='default' checkedChildren={"启用"} unCheckedChildren={"禁用"}
          loading={record.statusLoading} onChange={(checked: boolean) => {
            record.statusLoading = true;
            const newStatus = checked ? 1 : 2;
            changeStatus({ status: newStatus, id: record.id })
              .then(() => {
                record.status = newStatus;
                message.success(`成功修改状态`);
              })
              .catch(() => {
                message.error('修改状态失败');
              })
              .finally(() => {
                record.statusLoading = false;
              });
          }} />

      }
    }
  },
  {
    title: '账套',
    field: 'isMaster',
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.BooleanRadio,
    BoolRadioData: [
      { value: 'Y', label: '正式' },
      { value: 'N', label: '测试' }
    ],
    width: 80,
    slots: {
      default: ({ row }) => {
        const record = row;
        if (!Reflect.has(record, 'pendingStatus')) {
          record.pendingStatus = false;
        }
        return h(Switch, {
          size: 'default',
          checked: record.isMaster === 'Y',
          checkedChildren: '正式',
          unCheckedChildren: '测试',
          loading: record.pendingStatus,
          onChange(checked: boolean) {
            record.pendingStatus = true;
            const newStatus = checked ? 'Y' : 'N';
            changeIsMaster({ isMaster: newStatus, id: record.id })
              .then((res) => {
                record.isMaster = newStatus;
                message.success(res);
              })
              .catch((res) => {
                message.error(res);
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }
        });
      }
    }
  },
  {
    title: '提交',
    field: 'isSubmit',
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.BooleanRadio,
    BoolRadioData: [
      { value: 'Y', label: '是' },
      { value: 'N', label: '否' }
    ],
    width: 80,
    slots: {
      default: ({ row }) => {
        const record = row;
        if (!Reflect.has(record, 'pendingStatus')) {
          record.pendingStatus = false;
        }
        return h(Switch, {
          size: 'default',
          checked: record.isSubmit === 'Y',
          checkedChildren: '是',
          unCheckedChildren: '否',
          loading: record.pendingStatus,
          onChange(checked: boolean) {
            record.pendingStatus = true;
            const newStatus = checked ? 'Y' : 'N';
            changeIsSubmit({ isSubmit: newStatus, id: record.id })
              .then((res) => {
                record.isSubmit = newStatus;
                message.success(res);
              })
              .catch((res) => {
                message.error(res);
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }
        });
      }
    }
  },
  {
    title: '审核',
    field: 'isAudit',
    isRow: false,
    formHidden: false,
    fieldType: FieldTypeEnum.BooleanRadio,
    BoolRadioData: [
      { value: 'Y', label: '是' },
      { value: 'N', label: '否' }
    ],
    width: 80,
    slots: {
      default: ({ row }) => {
        const record = row;
        if (!Reflect.has(record, 'pendingStatus')) {
          record.pendingStatus = false;
        }
        return h(Switch, {
          size: 'default',
          checked: record.isAudit === 'Y',
          checkedChildren: '是',
          unCheckedChildren: '否',
          loading: record.pendingStatus,
          onChange(checked: boolean) {
            record.pendingStatus = true;
            const newStatus = checked ? 'Y' : 'N';
            changeIsAudit({ isAudit: newStatus, id: record.id })
              .then((res) => {
                record.isAudit = newStatus;
                message.success(res);
              })
              .catch((res) => {
                message.error(res);
              })
              .finally(() => {
                record.pendingStatus = false;
              });
          }
        });
      }
    }
  },
  {
    title: '备注',
    field: 'remark',
    isRow: true,
    formHidden: false,
    fieldType: FieldTypeEnum.Textarea
  },
  {
    width: 140,
    title: '操作',
    align: "left",
    slots: { default: 'action' },
    fixed: 'right'
  }
] as ColumnsFrom[];
